package com.redpack.quick.controller;

import com.redpack.quick.common.beans.Result;
import com.redpack.quick.consts.Constants;
import com.redpack.quick.datasource.entity.CashHistoryEntity;
import com.redpack.quick.datasource.entity.UserInfoEntity;
import com.redpack.quick.service.IUserInfo;
import com.redpack.quick.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * Created by shuttle on 4/3/18.
 */
@RestController
@RequestMapping(Constants.BASE_URL + "/user")
@Api(description = "用户信息相关")
public class UserInfoController extends BaseController {

    @Autowired
    private IUserInfo userInfo;

    @ApiOperation(value = "获取用户提现记录", notes = "获取用户提现记录", response = Result.class)
    @RequestMapping(value = "/getTxRecord", method = { RequestMethod.POST}, produces = "application/json;charset=utf-8")
    public Result<Boolean> getTxRecord(@RequestParam("uid") String uid, @RequestParam("token") String token){
        return successResult(userInfo.getCashHistory(uid, token));
    }

    @ApiOperation(value = "获取用户信息", notes = "获取用户信息", response = Result.class)
    @RequestMapping(value = "/getUserInfo", method = { RequestMethod.POST}, produces = "application/json;charset=utf-8")
    public Result<UserInfoEntity> getUserInfo(@RequestParam("id") String appId){
        return successResult(userInfo.findByAppId(appId));
    }

    @ApiOperation(value = "根据用户id获取用户信息", notes = "根据用户id获取用户信息", response = Result.class)
    @RequestMapping(value = "/getUserInfoById", method = { RequestMethod.POST}, produces = "application/json;charset=utf-8")
    public Result<UserInfoEntity> getUserInfo(@RequestParam("id") Long id,
                                              @RequestParam("token") String token){
        return successResult(userInfo.findByUserId(id, token));
    }

    @ApiOperation(value = "保存用户信息", notes = "保存用户信息", response = Result.class)
    @RequestMapping(value = "/save", method = { RequestMethod.POST}, produces = "application/json;charset=utf-8")
    public Result<UserInfoEntity> saveUserInfo(@RequestParam("token") String token,
                                               @RequestBody UserInfoEntity entity){
        return successResult(userInfo.save(entity, token));
    }

    @ApiOperation(value = "登录", notes = "登录", response = Result.class)
    @RequestMapping(value = "/login", method = { RequestMethod.POST}, produces = "application/json;charset=utf-8")
    public Result<Map<String, String>> login(@RequestParam String code,
                                @RequestParam String encryptedData,
                                @RequestParam String iv,
                                HttpServletRequest request){
        HttpSession session = request.getSession();
        Map<String, String> map = userInfo.login(code);
        session.setAttribute(map.get("openId"), DateUtils.getCurrentTimeStamp());
        return successResult(map);
    }

    @ApiOperation(value = "用户提现", notes = "用户提现", response = Result.class)
    @RequestMapping(value = "/transfer", method = { RequestMethod.POST}, produces = "application/json;charset=utf-8")
    public Result<String> getMoney(@RequestParam String uid,
                                             @RequestParam String token,
                                             @RequestParam BigDecimal quota,
                                             HttpServletRequest request){
        try {
            int result = userInfo.getMoney(uid, token, quota);
            String str = "提现成功！";
            if(result == 0){
                str = "提现失败！";
            }else if(result == 2){
                str = "提现金额不正确！";
            }
            return successResult(str);

        }catch(Exception e){
            return failResult();
        }

    }
}
